import Vue from 'vue'
import Vuex from 'vuex'
import {getAllTag, getAllType, getAllUser} from "../api/common";

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    AllUser: [],
    AllType: [],
    AllTag: [],
    SearchValue: '',
  },
  mutations: {
    SET_ALL_USER: (state, users) => {
      state.AllUser = users
    },
    SET_ALL_TYPE: (state, types) => {
      state.AllType = types
    },
    SET_ALL_TAG: (state, tags) => {
      state.AllTag = tags
    },
    SET_SEARCH_VALUE: (state, value) => {
      state.SearchValue = value
    },
  },
  actions: {
    GetAllUser({commit, state}) {
      return new Promise((resolve, reject) => {
        getAllUser().then(res => {
          commit("SET_ALL_USER", res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetAllType({commit, state}) {
      return new Promise((resolve, reject) => {
        getAllType().then(res => {
          commit("SET_ALL_TYPE", res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    GetAllTag({commit, state}) {
      return new Promise((resolve, reject) => {
        getAllTag().then(res => {
          commit("SET_ALL_TAG", res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  },
  modules: {},
  getters: {
    AllUser: state => state.AllUser,
    AllType: state => state.AllType,
    AllTag: state => state.AllTag,
    SearchValue: state => state.SearchValue,
  }
});

export default store
